﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Twi.COMMON.WCF
{
    public enum ResponseMessageInfoStatus
    {
        ServerException = 3 //服务器端未处理异常
        , ServerExceptionCatched = 31 //服务器端已捕获异常
        , ServerResultSuccess = 1 //无异常，达到预期目的
        , ServerResultUnsuccess = 2 //无异常，由于业务逻辑的原因，没有达到预期目的
        , ServerResultUnsuccessOKCancel = 4 //无异常，由于业务逻辑的原因，没有达到预期目的，选择是否进行二次操作
        , ServerResultUnsuccessYesNo = 7 
        , ClientVersionException = 99 //客户端版本异常
    }

    public class ResponseMessageInfo
    {
        private XElement _dataRoot;

        public XElement DataRoot
        {
            get
            {
                XElement responseStatus = new XElement("ResponseStatus");
                responseStatus.SetElementValue("Status", (int)_status);
                responseStatus.SetElementValue("Message", _message);
                responseStatus.SetElementValue("Exception", _exception);
                if (_dataRoot.Element("ResponseStatus") != null)
                {
                    _dataRoot.Element("ResponseStatus").Remove();
                }
                _dataRoot.Add(responseStatus);
                return _dataRoot;
            }
        }

        public ResponseMessageInfo()
        {
            _dataRoot = new XElement("DataRoot");
            _status = ResponseMessageInfoStatus.ServerResultSuccess;
            _message = "";
            _exception = "";
            _clientVersion = Launch.Element("Version").GetValue();  //版本号
        }

        public void PopulateResponseData(params XElement[] args) 
        {
            XElement responseData = _dataRoot.Element("ResponseDate") == null ? new XElement("ResponseData") : _dataRoot.Element("ResponseData");
            responseData.Add(args);
            _dataRoot.Add(responseData);
        }
        
        private ResponseMessageInfoStatus _status;
        public ResponseMessageInfoStatus Status { get { return _status; } set { _status = value; } }
        private string _message;
        public string Message { get { return _message; } set { _message = value; } }
        private string _exception;
        public string Exception { get { return _exception; } set { _exception = value; } }

        private string _clientVersion = "";
        public string ClientVersion { get { return _clientVersion; } set { _clientVersion = value; } }
        #region 获得系统文件及版本信息
        private static XElement _Launch = null;
        /// <summary>
        /// 系统文件及版本信息
        /// </summary>
        public static XElement Launch
        {
            get
            {
                if (_Launch == null)
                {
                    _Launch = GetSysVersion();
                }
                return _Launch;
            }
            set
            {
                _Launch = value;
            }
        }

        /// <summary>
        /// 获得系统文件及版本信息
        /// </summary>
        /// <returns></returns>
        private static XElement GetSysVersion()
        {
            XElement xeLaunch = null;
            try
            {
                string launchPath = System.AppDomain.CurrentDomain.BaseDirectory;
                string launchInfoFileName = @"Launch\Launch.xml";
                string localPath = launchPath + launchInfoFileName;  //绝对路径
                if (System.IO.File.Exists(localPath))
                {
                    System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
                    xmlDoc.Load(localPath);
                    xeLaunch = XElement.Parse(xmlDoc.InnerXml);
                }
            }
            finally
            {
                if (xeLaunch == null)
                {
                    xeLaunch = new XElement("Launch");
                }
            }
            return xeLaunch;
        }
        #endregion 

    }
}
